﻿<%@ WebHandler Language="C#" Class="MESCommonBase" %>

using System;
using System.Web;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Web.Script.Serialization;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Net.Mime;
using System.Timers;
using System.Xml;

public class MESCommonBase : IHttpHandler
{
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    public int message;
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string senderServerIp = HttpContext.Current.Request["senderServerIp"];
        string fromMailAddress = HttpContext.Current.Request["fromMailAddress"];
        string toMailAddress = HttpContext.Current.Request["toMailAddress"];
        string subjectInfo = HttpContext.Current.Request["subjectInfo"];
        string bodyInfo = HttpContext.Current.Request["bodyInfo"];
        string mailUsername = HttpContext.Current.Request["mailUsername"];
        string mailPassword = HttpContext.Current.Request["mailPassword"];
        string enclosure = HttpContext.Current.Request["enclosure"];
        try
        {
            sendMail(toMailAddress, fromMailAddress, bodyInfo, subjectInfo, senderServerIp, mailUsername, mailPassword, enclosure);
            if (message == 1)
            {
                context.Response.Write(message);
            }
            else {
                context.Response.Write(message);
            }

        }
        catch (Exception ex)
        {
            return;
        }
    }
    private void sendMail(string _toMailAddress, string _fromMailAddress, string _bodyInfo, string _subjectInfo, string _senderServerIp, string _mailUsername, string _mailPassword, string _enclosure)
    {
        string toMailAddress = _toMailAddress;
        string fromMailAddress = _fromMailAddress;
        string bodyInfo = _bodyInfo;
        string subjectInfo = _subjectInfo;
        string senderServerIp = _senderServerIp;
        string mailUsername = _mailUsername;
        string mailPassword = _mailPassword;
        string enclosure = _enclosure;


        string address = "";
        string displayName = "";
        //string w_txt收件人 = txt收件人.Text.Trim();

        if (toMailAddress == "")
        {
            message = 3; // 未填写收件人地址
            return;
        }
        MailAddress from = new MailAddress(fromMailAddress);
        MailMessage newMailMessage = new MailMessage();

        string[] mailNames = (toMailAddress).Split(',');
        {
            try
            {
                foreach (string name in mailNames)
                {
                    if (name != string.Empty)
                    {
                        if (name.IndexOf('<') > 0)
                        {
                            displayName = name.Substring(0, name.IndexOf('<'));
                            address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');

                        }
                        else
                        {
                            displayName = string.Empty;
                            address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
                        }

                    }
                    newMailMessage.From = from;

                    newMailMessage.To.Add(new MailAddress(address, displayName));



                    newMailMessage.Body = bodyInfo;
                    newMailMessage.Subject = subjectInfo;



                    //设置SMTP服务器地址 
                    SmtpClient newclient = new SmtpClient(senderServerIp);   //以新浪为例（126的用不了，也不晓得为啥）
                    newclient.UseDefaultCredentials = false;
                    //此处设置发件人邮箱的用户名和密码 
                    newclient.Credentials = new System.Net.NetworkCredential(mailUsername, mailPassword); //发件人的账号和密码
                    newclient.DeliveryMethod = SmtpDeliveryMethod.Network;
                    if (enclosure != "")
                    {
                        newMailMessage.Attachments.Add(new Attachment(enclosure));          // 发送附件
                    }
                    newMailMessage.Priority = MailPriority.High;  //设置发送级别
                                                                  //发送邮件 
                    newclient.Send(newMailMessage);

                }
                message = 1; // 邮件发送完毕！
            }
            catch (Exception exp)
            {
                message = 2; // 邮件发送发生错误
            }
        }

    }
}